import _ from 'lodash';
import { TLoginInfo } from '@/models';
// import * as SecureStore from 'expo-secure-store';
import { TSubscription } from '@/types';

const encryptionKey = 'duom';

export namespace Storage {
  //登录监听器
  const listeners: ((info?: TLoginInfo) => void)[] = [];
  //存储变更监听器
  const lsrs: { [key: string]: ((value: any) => void)[] } = {};
  // let pub: MMKV = new MMKV({
  //   id: 'duom.storage.public',
  //   encryptionKey,
  // });
  // let pri: MMKV = new MMKV({
  //   id: 'duom.storage.logined',
  //   encryptionKey,
  // });
  let loginInfo: TLoginInfo | undefined;
  // if (__DEV__) {
  //在flipper上查看mmkv里面的内容
  // require('react-native-mmkv-flipper-plugin').initializeMMKVFlipper({
  //   logined: pri,
  //   public: pub,
  // });
  // require('react-native-performance-flipper-reporter').setupDefaultFlipperReporter();
  // }

  export function setLoginInfo(info?: TLoginInfo) {
    listeners.forEach(lsr => {
      lsr(info);
    });

    // loginInfo = info;
    // if (info) {
    //   set('login.userinfo', info);
    // } else {
    //   clear();
    // }
  }

  export function getLoginInfo() {
    // if (!loginInfo) {
    //   loginInfo = get<TLoginInfo | undefined>('login.userinfo');
    // }
    // return loginInfo;
  }

  export function set(key: string, value: any, isPublic?: boolean) {
    // const mmkv = isPublic ? pub : pri;
    // mmkv.set(key, JSON.stringify({ [key]: value }));
    // const lsrsObjct = lsrs[key];
    // if (Array.isArray(lsrsObjct)) {
    //   lsrsObjct.forEach(lsr => lsr(value));
    // }
  }

  export function get<T>(key: string, isPublic?: boolean) {
    // const mmkv = isPublic ? pub : pri;
    // const str = mmkv.getString(key);
    // try {
    //   const res = JSON.parse(str!);
    //   return res[key] as T;
    // } catch {
    //   return str as T;
    // }
  }

  export function remove(key: string, isPublic?: boolean) {
    // const mmkv = isPublic ? pub : pri;
    // mmkv.delete(key);
  }

  export function clear(isPublic?: boolean) {
    // const mmkv = isPublic ? pub : pri;
    // mmkv.clearAll();
  }

  export function addLoginStatusListener(
    lsr: (info?: TLoginInfo) => void
  ): TSubscription {
    listeners.push(lsr);
    return {
      remove: () => {
        _.pull(listeners, lsr);
      },
    };
  }

  export function on(key: string, lsr: (value: any) => void) {
    if (lsrs[key]) {
      lsrs[key].push(lsr);
    } else {
      lsrs[key] = [lsr];
    }
    return {
      off: () => {
        _.pull(lsrs[key], lsr);
      },
    };
  }

  export function keys(isPublic: boolean = false) {
    // const mmkv = isPublic ? pub : pri;
    // return mmkv.getAllKeys();
  }

  /* 存当前时间 */
  export function setTefreshTokenATime() {
    const time = new Date().getTime();
    set('resetTokenTime', time);
  }
  /* 取当前时间 */
  export function getRefreshTokenTime() {
    // return get('resetTokenTime') as string;
  }
  /**
   * 距离上次时间满足6天时间
   * 6天调用一次生成新token
   * */
  export function isRefreshTokenTime() {
    // const time = new Date().getTime();
    // const time2 = getRefreshTokenTime();
    // if (!time2) {
    //   return true;
    // }
    // const temporal = time - Number(time2);
    // // 6天调用一次生成新token
    // const leave = 6 * 24 * 3600 * 1000;
    // if (temporal > leave) {
    //   return true;
    // }
    // return false;
  }

  /**
   * 设置摇一摇开关
   */
  export function setFeedbackSwitch(isSwitch?: boolean) {
    set('feedbackSwitch', isSwitch, true);
  }
  /**
   * 获取摇一摇开关
   */
  export function getFeedbackSwitch() {
    return get('feedbackSwitch', true);
  }
}
